package com.zx.mes.rabbitmq.hyl.send;

import com.zx.mes.rabbitmq.entity.Message;
import com.zx.mes.rabbitmq.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 消息发送配置
 * @author huayunlong
 * @date 2018-8-6
 */
@Component
@Slf4j
public class RabbitSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;



    final RabbitTemplate.ReturnCallback returnCallback = (message, replyCode, replyText, exchange, routingKey) -> {
        log.debug("ReturnCallback==>  message:[{}]  ,replyCode:[{}]  ,replyText:[{}]  ,exchange:[{}]  ,routingKey:[{}]"
                ,message, replyCode, replyText, exchange, routingKey);
    };

    final RabbitTemplate.ConfirmCallback confirmCallback = (correlationData, ack, cause) -> {
        log.debug("ConfirmCallback==>  correlationData:[{}], ack:[{}],, cause:[{}]",correlationData, ack, cause);
    };

    public void sender(Message<Order> message){
        this.rabbitTemplate.setReturnCallback(returnCallback);
        this.rabbitTemplate.setConfirmCallback(confirmCallback);

        this.rabbitTemplate.convertAndSend("test.exchange2"
                ,"test2.send",message,new CorrelationData(UUID.randomUUID().toString()));
    }

    public void senderOrder(Order order){
        this.rabbitTemplate.setReturnCallback(returnCallback);
        this.rabbitTemplate.setConfirmCallback(confirmCallback);

        this.rabbitTemplate.convertAndSend("test.exchange"
                ,"test.send",order,new CorrelationData(UUID.randomUUID().toString()));
    }
}
